
//
//  HTStatsViewModel.swift
//  weibo
//
//  Created by JonsonLiu on 16/4/21.
//  Copyright © 2016年 liujonson. All rights reserved.
//

import UIKit
import SDWebImage
class HTStatsViewModel: NSObject {

    var status : [HTStatsCellViewModel]?
    
    
    
    //下拉刷新
    
    
    func loadData(isPullp : Bool,finished : (isSuccess : Bool)->()){
        
        let uraString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        var max_id : Int64 = 0
        //var since_id : Int64 = 0
        
        if isPullp{
            
            max_id = status?.last?.status?.id ?? 0
            
            if max_id != 0{
                max_id--
            }
        }else{
           // since_id = status?.first?.status?.id ?? 0
        }
        
        
        
        
        
        let prams = [
        
            "access_token": HTViewModel.sharedViewModel.accessToken ?? "","max_id" : "\(max_id)"//,"since_id" : "\(since_id)"
        ]
        
        
        NetworkTools.sharedTools.request(.Get, urlString: uraString, parameters: prams) { (responseObject, error) -> () in
            
            if error != nil {
                
                finished(isSuccess: false)
                return
            }
//            print(responseObject)
            guard let responseData = responseObject as? [String : AnyObject] else {
            
                finished(isSuccess: false)
                return
                
            }
            
            guard let dicts = responseData["statuses"] as? [[String : AnyObject]] else {
            
                finished(isSuccess: false)
                return
            }
            
            var tempArray = [HTStatsCellViewModel]()
            
            for value in dicts{
            
                let  stuts = HTStats(dict : value)
                let viewModel = HTStatsCellViewModel()
                viewModel.status = stuts
                tempArray.append(viewModel)
            }
            
            
            if self.status == nil {
                self.status = [HTStatsCellViewModel]()
            }
            
            if isPullp{
            
            self.status = self.status! + tempArray
            }else{
            
                self.status = tempArray + self.status!
            }
            //finished(isSuccess: true)
            
            self.cachSingleImage(tempArray, finished: finished)
            
        }
        
    }
    
    //缓存单张图片
    private func cachSingleImage(array : [HTStatsCellViewModel], finished : (isSuccess : Bool) -> ()){
    
        let group = dispatch_group_create()
        
        for value in array{
        
            let originalPicUrl = value.status?.pic_urls
            let retweetPicUrl = value.status?.retweeted_status?.pic_urls
            
            let  picUrl = (originalPicUrl == nil || originalPicUrl?.count == 0) ? retweetPicUrl : originalPicUrl
            
            
            if picUrl?.count == 1{
            
                let  urlString  = picUrl?.first?.thumbnail_pic

            
            dispatch_group_enter(group)
            
              SDWebImageManager.sharedManager().downloadImageWithURL(NSURL(string: urlString ?? ""), options: [], progress: nil,
                completed: { (image, error, _, _, _) -> Void in
                    
                    value.updateSingleImagePictureView()
                    
                    dispatch_group_leave(group)
                    
              })
                
            }
        }
        
        
        dispatch_group_notify(group, dispatch_get_main_queue()) { () -> Void in
            
            finished(isSuccess: true)
        }
    }
    
}
